package com.ruoyi.supply.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.supply.domain.TMemberConsumeLog;
import com.ruoyi.supply.domain.base.BaseInfoEntity;
import com.ruoyi.supply.domain.dto.ConsumeLogDto;
import com.ruoyi.supply.domain.dto.ConsumeLogSearchDto;
import com.ruoyi.supply.enums.PayStatusEnum;
import com.ruoyi.supply.mapper.TMemberConsumeLogMapper;
import com.ruoyi.supply.mapper.TMemberLogMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author huangxiong
 * @date 2025/06/15 17:07
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class MemberLogService {

    private final TMemberLogMapper tMemberLogMapper;
    private final TMemberConsumeLogMapper tMemberConsumeLogMapper;

    public List<BaseInfoEntity> getCheckRecord(Long memberId){
        return tMemberLogMapper.getCheckRecord(memberId);
    }

    public List<ConsumeLogDto> getConsumeRecord(ConsumeLogSearchDto consumeLogSearchDto){
        return tMemberConsumeLogMapper.getConsumeRecord(consumeLogSearchDto);
    }

    public List<TMemberConsumeLog> getGradeConsumeRecord(Long memberId,String param) {
        return tMemberConsumeLogMapper.getGradeConsumeRecord(memberId,param);
    }
    
    public List<BaseInfoEntity> getInfoConsumeRecord(Long memberId,String param,String status){
        return tMemberConsumeLogMapper.getInfoConsumeRecord(memberId,param,status);
    }

    public PayStatusEnum getPayStatus(String orderNo){
        TMemberConsumeLog tMemberConsumeLog = tMemberConsumeLogMapper.selectOne(new QueryWrapper<TMemberConsumeLog>().lambda().eq(TMemberConsumeLog::getOrderNo, orderNo).last("limit 1"));
        if(tMemberConsumeLog == null){
            return null;
        }
        return PayStatusEnum.getByCode(tMemberConsumeLog.getStatus());
    }

    public void updatePayStatus(String orderNo, PayStatusEnum payStatusEnum,String remark) {
        TMemberConsumeLog tMemberConsumeLog = new TMemberConsumeLog();
        tMemberConsumeLog.setStatus(payStatusEnum.getCode());
        tMemberConsumeLog.setRemark(remark);
        tMemberConsumeLogMapper.update(tMemberConsumeLog,
                new QueryWrapper<TMemberConsumeLog>().lambda().eq(TMemberConsumeLog::getOrderNo, orderNo)
        );
    }

    public List<String> getIngPayOrderNo() {
        return tMemberConsumeLogMapper.selectList(
                new QueryWrapper<TMemberConsumeLog>().lambda().eq(TMemberConsumeLog::getStatus, PayStatusEnum.ING.getCode())
        ).stream().map(TMemberConsumeLog::getOrderNo).collect(Collectors.toList());
    }


}
